package android.framework.paging;

import android.content.Context;
import android.content.Intent;
import android.framework.reflaction.injectors.OnClickListenerInjector;


public abstract class BaseViewModel <TView extends BaseViewInitializer> implements IInitListener{
	
	private TView view;
	protected Context context;
	
	
	public BaseViewModel(Context context){
		this.context = context;
		
	}
	
	public abstract void onInit();
	
	public void init(){
		new OnClickListenerInjector().Inject(view.getView(), this);
		onInit();
	}
	
	
	
	public void startActivity(Class<?> activityType){
		startActivity(activityType, null);
	}
	
	public void startActivity(Class<?> activityType, Object obj){
		if(obj != null){
			ObjectHolder.getInstance().addObject(activityType, obj);
		}
		context.startActivity(new Intent(context, activityType));
	}
	
	
	/*
	 * 
	 try {
		} catch (IllegalArgumentException e) {
			throw new BindingException(String.format("can't attached view and model"), e);
		} catch (IllegalAccessException e) {
			throw new BindingException(String.format("can't attached view and model"), e);
		} catch (SecurityException e) {
			throw new BindingException(String.format("can't attached view and model"), e);
		} catch (NoSuchFieldException e) {
			throw new BindingException(String.format("can't attached view and model"), e);
		}
	  @SuppressWarnings("unchecked")
	private <T> void binding1(){
		Class<?> cls = getClass();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			BindingFieldAnnotation annotation = field.getAnnotation(BindingFieldAnnotation.class);
			if(annotation != null){
				String fieldName = annotation.viewFieldName();
				try {
					Field vmField = view.getClass().getField(fieldName);
					vmField.setAccessible(true);
					field.setAccessible(true);
					IBindable<T> currentBindable = (IBindable<T>)field.get(this);
					IBindable<T> vmBindable = (IBindable<T>)vmField.get(view);
					Binding.attached(currentBindable, vmBindable);
				} catch (IllegalArgumentException e) {
					throw new BindingException(String.format("can't attached %s and %s",  fieldName, field.getName()), e);
				} catch (IllegalAccessException e) {
					throw new BindingException(String.format("can't attached %s and %s",  fieldName, field.getName()), e);
				} catch (SecurityException e) {
					throw new BindingException(String.format("can't attached %s and %s",  fieldName, field.getName()), e);
				} catch (NoSuchFieldException e) {
					throw new BindingException(String.format("can't attached %s and %s",  fieldName, field.getName()), e);
				}
			}
		}
	}
	
	
	
	@SuppressWarnings("unchecked")
	private <T> void binding() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException{
		Class<?> cls = getClass();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			BindingFieldAnnotation annotation = field.getAnnotation(BindingFieldAnnotation.class);
			if(annotation != null){
				field.setAccessible(true);
				Object model = field.get(this);
				Class<?> modelClass = model.getClass();
				
				String viewName = annotation.viewFieldName();
				String modelName = annotation.modelFieldName();
				
				Field modelField = modelClass.getField(modelName);
				Field viewField = view.getClass().getField(viewName);
				viewField.setAccessible(true);
				modelField.setAccessible(true);
				
				IBindable<T> modelBindable = (IBindable<T>)modelField.get(model);
				IBindable<T> viewBindable = (IBindable<T>)viewField.get(view);
				Binding.attached(modelBindable, viewBindable);
			}
		}
	}*/

	protected TView getView() {
		return view;
	}

	public void setView(TView view) {
		this.view = view;
	}
}
